package com.zhongyu.controller;

import com.zhongyu.pojo.Order;
import com.zhongyu.pojo.OrderDetail;
import com.zhongyu.pojo.StoreDetail;
import com.zhongyu.service.ChuKuService;
import com.zhongyu.service.OrderDetailService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/out/sal/")
public class ChuKuController {
    @Resource
    private ChuKuService chuKuService;
    @Resource
    private OrderDetailService orderDetailService;

    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize){
        PageResult pageResult=chuKuService.searchAll(pageNo, pageSize);
        return ResultVo.success(pageResult);
    }

    @GetMapping("/search")
    public ResultVo search(Integer id){
        List<Order> pageResult=chuKuService.searchAll(id);
        return ResultVo.success(pageResult);
    }

    @GetMapping("/searchAll")
    public ResultVo searchAll(Integer id){
        List<Order> pageResult=chuKuService.search(id);
        return ResultVo.success(pageResult);
    }
    @GetMapping("/searchOut")
    public ResultVo searchRu(Integer id){
        OrderDetail pageResult=orderDetailService.searchOut(id);
        return ResultVo.success(pageResult);
    }
    @PostMapping("/add")
    public ResultVo add(@RequestBody StoreDetail storeDetail, HttpSession session){
        try {

            orderDetailService.addOut(storeDetail,session);
            return ResultVo.success("出库成功！");
        }catch (Exception | ResultVo ex){
            return (ResultVo) ResultVo.failure("出库库存不足！");
        }
    }

}
